// pages/TradingAreaCenter/shopCircleManage.js
const controller=require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    name:"",
    index4:0,
    shopList:[],
    index:"",
    shopGroupId:"",
    selectIndex:"",
    timer:'',
  },

  toAddShopCircle(){
    wx.navigateTo({
      url: '/pages/TradingAreaCenter/addShopCircle',
    })
  },
  toShopCircleList(){
    wx.navigateTo({
      url: '/pages/TradingAreaCenter/shopCircleList',
    })
  },

  todetail(e){
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/TradingAreaCenter/shopCircleDetail?id='+id,
    })
  },

  //获取商圈名称
  getShopCircleName(e){
    this.setData({
      name:e.detail.value
    })
    if(this.timer)clearTimeout(this.timer)
    this.timer = setTimeout(() => {
      this.getShopCircleList()
    }, 500);
  },

  //选择店铺
  bindPickerChange(e){
    let id=this.data.shopList[e.detail.value].id
    let haveShopList=this.data.list[this.data.selectIndex].shopIds.split(",")
    let isHave=false
    haveShopList.forEach(item=>{
      if(id==item){
        isHave=true
      }
    })
    if(isHave==true){
      wx.showToast({
        icon:"none",
        title: '商圈内已存在该店铺',
      })
    }else{
      this.addShop(id)
    }
  },

  //添加店铺
  addShop(id){
    let value={}
    value.agentId=wx.getStorageSync('agentId')
    value.shopGroupId=this.data.shopGroupId
    value.shopIds=id
    controller.addShop(value).then(res=>{
      wx.showToast({
        icon:"none",
        title: '操作成功'
      })
      this.getShopCircleList()
    })
  },

  //选择商圈
  selectShopGroup(e){
    this.setData({
      shopGroupId:e.currentTarget.dataset.id,
      selectIndex:e.currentTarget.dataset.index
    })
  },

  //获取店铺列表
  getShopList(){
    controller.myShop().then(res=>{
      this.setData({
        shopList:res.data
      })
    })
  },

  bindShopCircle(e){
    let id=this.data.list[parseInt(e.detail.value)].id
    console.log(id)
    wx.navigateTo({
      url: '/pages/TradingAreaCenter/setTopPictures?id='+id,
    })
  },

  //获取商圈列表
  getShopCircleList(){
    let value={}
    value.name=this.data.name
    controller.myShopGroupList(value).then(res=>{
        this.setData({
          list:res.data
        })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getShopCircleList()
    this.getShopList()
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})